﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AlgoImpl
{
    public class Statictics :IComparable
    {
        private List<WorkerStat> m_WorkersStat;
        private double m_TotalRank = 0;

        
        public Statictics(Dictionary<string, List<string>> req, Dictionary<string, List<string>> result)
        {
            m_WorkersStat = new List<WorkerStat>();
            initializeWorkersStat(req, result);
            calculateTotalRank();
        }

        public WorkerStat GetStatOfWorker(string worker)
        {
            int indexFound = findWorkerStatOfWorker(worker);
            if (indexFound != -1)
            {
                return m_WorkersStat[indexFound];
            }
            else
            {
                throw new Exception("worker " + worker + " Not Found");
            }
        }

        public void ClearAllStat()
        {
            foreach (WorkerStat ws in m_WorkersStat)
            {
                ws.Clean();
            }
        }

        public double TotalRank { get { return m_TotalRank; } }

        private void calculateTotalRank()
        {
            foreach (WorkerStat workerStat in m_WorkersStat)
            {
                m_TotalRank += workerStat.Rank;
            }
            m_TotalRank /= m_WorkersStat.Count;
        }

        private void initializeWorkersStat(Dictionary<string, List<string>> req, Dictionary<string, List<string>> result)
        {
            foreach (string worker in req.Keys)
            {
                m_WorkersStat.Add(new WorkerStat(req[worker], result[worker], worker));
            }
        }

        private int findWorkerStatOfWorker(string worker)
        {
            int foundIndex = -1;
            for (int i = 0; i < m_WorkersStat.Count; i++)
            {
                if (m_WorkersStat[i].Worker.Equals(worker))
                {
                    foundIndex = i;
                    break;
                }
            }
            return foundIndex;
        }

        public int CompareTo(object obj)
        {
            if (obj.GetType() == typeof(Statictics))
            {
                int isEqual = 0;
                Statictics s = obj as Statictics;
                if (m_TotalRank > s.TotalRank)
                {
                    isEqual = 1;
                }
                else if (m_TotalRank < s.TotalRank)
                {
                    isEqual = -1;
                }
                return isEqual;
            }
            else
            {
                throw new Exception("");
            }
        }
    }
}
